package com.sayice.robotconflict.game.object.property;

/**
 * 可回退的可见坐标
 *
 * @author shubing
 */
public class RewindableVolatileCoord extends VolatileCoord implements RewindableCoord {
    /**
     * 上一个x坐标
     */
    private volatile Double prevX;
    
    /**
     * 上一个y坐标
     */
    private volatile Double prevY;
    
    public RewindableVolatileCoord(double x, double y) {
        super(x, y);
    }
    
    public RewindableVolatileCoord(double x, double y, double prevX, double prevY) {
        super(x, y);
        this.prevX = prevX;
        this.prevY = prevY;
    }
    
    public RewindableVolatileCoord(Coord coord) {
        super(coord);
    }
    
    public RewindableVolatileCoord(Coord coord, Coord prevCoord) {
        super(coord);
        this.prevX = prevCoord.getX();
        this.prevY = prevCoord.getY();
    }
    
    @Override
    public Double getPrevX() {
        return prevX;
    }
    
    @Override
    public Double getPrevY() {
        return prevY;
    }
    
    @Override
    public void setX(double x) {
        prevX = this.x;
        super.setX(x);
    }
    
    @Override
    public void setY(double y) {
        prevY = this.y;
        super.setY(y);
    }
    
    @Override
    public void set(double x, double y) {
        prevX = this.x;
        prevY = this.y;
        super.set(x, y);
    }
    
    @Override
    public void set(Coord coord) {
        prevX = this.x;
        prevY = this.y;
        super.set(coord);
    }
    
    @Override
    public void addX(double value) {
        prevX = this.x;
        super.addX(value);
    }
    
    @Override
    public void addY(double value) {
        prevY = this.y;
        super.addY(value);
    }
    
    @Override
    public void rewind(Double pPrevX, Double pPrevY) {
        if (prevX == null || prevY == null) {
            return;
        }
        x = prevX;
        y = prevY;
        prevX = pPrevX;
        prevY = pPrevY;
    }
    
    @Override
    public String toString() {
        return "RewindableVolatileCoord{" +
                "prevX=" + prevX +
                ", prevY=" + prevY +
                ", x=" + x +
                ", y=" + y +
                '}';
    }
}
